package com.health.controller;

import com.health.pojo.TableColumn;
import com.health.pojo.Users;
import com.health.service.HealthComboService;
import com.health.service.UserService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

// 选中用户的体检套餐下拉框
@WebServlet("/searchDataByUser")
public class SearchDataByUserServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        UserService userService = new UserService();
        HealthComboService healthComboService = new HealthComboService();
        TableColumn tableColumn;
        int type = Integer.parseInt(request.getParameter("type"));
        if (type == 1) {
            tableColumn = new TableColumn("套餐编号", "套餐名", "日期", "用户Id");
            request.setAttribute("column", tableColumn);
            String userId = request.getParameter("userId");     // 获取选中的userId
            List<Users> usersListByCombo = userService.searchDataByCombo(Integer.parseInt(userId));
            request.setAttribute("userId", userId);      // 存入用户Id用于第二次下拉框请求
            request.setAttribute("usersListByCombo", usersListByCombo);
        } else {
            tableColumn = new TableColumn("套餐编号", "套餐名", "项目编号", "项目名");
        }


        // 查询出所有用户信息封装到User实体（返回list集合）
        List<Users> usersList = userService.searchUserInformation();


        request.setAttribute("index", request.getParameter("index"));
        // 将集合数据放到请求域中
        request.setAttribute("usersList", usersList);


        // 设置侧边栏状态
        request.setAttribute("asideStatus", 1);
        request.setAttribute("openInputView", -1);


        request.getRequestDispatcher("restrictedPages/admin.jsp").forward(request, response);


    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }
}
